/**
 * 派单绩效
 *
 * @author HandsomeHan
 * @date 2018/12/7 13:46:38
 */
layui.config({
    base: "/static/js/module/"
}).extend({
    select: '{/}/static/js/module/perf/perf.select'
}).define(['index', 'table', 'laydate', 'util', 'form', 'select'], function (exports) {
    var table = layui.table,
        date = layui.laydate,
        form = layui.form,
        util = layui.util,
        param = {},
        $ = layui.jquery,
        select = layui.select,
        now = new Date(),
        startDate = new Date(now.getFullYear(), now.getMonth(), 1),
        startDateIns = date.render({
            elem: '#startDatePuppet'
            , trigger: 'click'
            , value: util.toDateString(startDate, "yyyy-MM-dd")
            , isInitValue: true
            , btns: ['now', 'confirm']
            , done: function (value, date, endDate) {
                $('#startDate').val(value);
            }
        }),
        endDateIns = date.render({
            elem: '#endDatePuppet'
            , trigger: 'click'
            , value: util.toDateString(now, "yyyy-MM-dd")
            , max: 0
            , isInitValue: true
            , btns: ['now', 'confirm']
            , done: function (value, date, endDate) {
                $('#endDate').val(value);
            }
        });
    $('#startDate').val($('#startDatePuppet').html());
    $('#endDate').val($('#endDatePuppet').html());
    let cols = [[
        {field: 'distributorName', title: '派单员', align: 'center'},
        {field: 'confirmCount', title: '确认', align: 'center'},
        {field: 'distributeCount', title: '派单', align: 'center'},
        {field: 'distributedCount', title: '派单过', align: 'center'},
        {field: 'remarkCount', title: '备注', align: 'center'},
        {field: 'remarkedCount', title: '备注过', align: 'center'},
        {field: 'trackCount', title: '跟单', align: 'center'},
        {field: 'trackedCount', title: '跟单过', align: 'center'},
        {field: 'distributeSuccessCount', title: '派单成功', align: 'center'},
        {field: 'abnormalHandlingCount', title: '异常完成', align: 'center'},
        {field: 'complaintHandlingCount', title: '投诉处理', align: 'center'}
    ]];
    let url = {
        tableData: "/perf/distribute/loadData.action",
        export: "/perf/distribute/export.action",
        distributor: '/perf/distribute/listDistributorByPlat.action'
    };

    let tableOption = {
        elem: '#dataTable'
        , url: url.tableData
        , height: "full-100"
        , cols
        , page: true
        , limit: 50
        , limits: [50, 100, 200]
        , where: {
            from: util.toDateString(startDate, "yyyy-MM-dd"),
            to: util.toDateString(now, "yyyy-MM-dd")
        }
        , loading: true
        , toolbar: true
        , defaultToolbar: []
        , done: function (res) {
            //添加按钮
            ReportUtil.toobarRightAddButton([{
                title: '帮助',
                layEvent: 'CUSTOM_HELP',
                icon: 'layui-icon-help'
            }, {
                title: '导出',
                layEvent: 'CUSTOM_EXPORT',
                icon: 'layui-icon-export',
                display: hasExport
            }]);
            ReportUtil.renderTotalRow(res.extData);
            // 修复reload的时候会携带上次查询参数
            param = this.where;
            document.tableIns = this;
        }
        , request: {
            pageName: 'pageIndex' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusCode: 200
        }
    };
    let tabObj = table.render(tableOption);

    form.on('submit(query-submit)', function (data) {
        document.tableIns.where = {};
        tabObj.reload({
            where: data.field,
            page: {curr: 1}
        });
        return false;
    });
    form.on('submit(zmn-form-reset)', function () {
        location.reload();
    });
    table.on('toolbar(dataTable)', function (obj) {
        switch (obj.event) {
            case 'CUSTOM_EXPORT':
                let that = this;
                let ele = that.querySelector('i');
                ReportUtil.toggleLoading(ele);
                that.setAttribute('lay-event', '');
                zmnUtil.download(url.export, param, "派单绩效" + new Date().toISOString() + '.xls', (res) => {
                    that.setAttribute("lay-event", 'CUSTOM_EXPORT');
                    ReportUtil.toggleLoading(ele);
                });
                break;
            case "CUSTOM_HELP":
                ReportUtil.tips(help_content.innerHTML, this);
                break;
        }
    });
    select.render({
        elem: '#plat',
        child: {
            elem: '#distributorId',
            url: url.distributor,
            param: 'plat'
        }
    });
    select.load({
        elem: '#distributorId',
        url: url.distributor
    });
    exports('oms_perf_distribute');
});
